package plug.QCMPlugin;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import view.Fenetre;
import view.Param;

import Controller.EtudiantController;
import Controller.MainController;
import Model.Exercice;
import Model.Resultat;

@SuppressWarnings("serial")
public class QCMPanelEtud extends JPanel implements ActionListener {

	private MainController control;
	private Exercice exo;

	private ArrayList<JCheckBox> cList;
	private JButton bEnter;

	public QCMPanelEtud(MainController control, Exercice exo) {

		// Initialisation des variables
		this.control = control;
		this.exo = exo;

		ArrayList<String> choix = split(exo.getEnonce().getContenu());

		// Alignement vertical des conteneurs
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// Creation des conteneurs
		JLabel lQuestion = new JLabel(choix.get(0));
		JPanel pChoix = new JPanel();
		cList = new ArrayList<JCheckBox> ();
		bEnter = new JButton("Valider");
		
		// Ajustement du panel de choix
		pChoix.setLayout(new BoxLayout(pChoix, BoxLayout.Y_AXIS));
		
		for (int i=1; i<choix.size(); i++) {
			cList.add(new JCheckBox(choix.get(i)));
			pChoix.add(cList.get(i-1));
		}
		
		// Ajout de la transparence
		this.setOpaque(false);
		pChoix.setOpaque(false);

		// Ajout des conteneurs au panel
		this.add(lQuestion);
		this.add(pChoix);
		this.add(bEnter);
		
		// Ajout des marges
		pChoix.setBorder(Param.shortMargin);
		

		// Ajout des ecouteurs au panel
		bEnter.addActionListener(this);

	}

	/**Cette methode decompose une chaine de caracteres a chaque virgule.**/
	public ArrayList<String> split(String chaine) {

		ArrayList<String> liste = new ArrayList<String> ();
		String subWord = "";

		for (int i=0; i<chaine.length(); i++) {
			if (chaine.charAt(i) != ',') {
				subWord += chaine.charAt(i);
			}
			else {
				liste.add(subWord);
				subWord = "";
			}
		}
		liste.add(subWord);

		return liste;

	}

	public void actionPerformed(ActionEvent event) {

		if (event.getSource() == bEnter ) {

			bEnter.setEnabled(false);

			QCM qcm = (QCM) exo;
			EtudiantController e = control.getEtudiantController();
			Resultat r = new Resultat(qcm, e.getEtudiant());


			ArrayList<String> reponses = split(exo.getCorrection().getContenu());

			int points = 0;

			for (int i=0; i<cList.size(); i++) {

				for (int j=0; j<reponses.size(); j++) {

					if (cList.get(i).isSelected()) {

						if (cList.get(i).getText().equals(reponses.get(j))) { // Si l'eleve a trouve une reponse
							cList.get(i).setForeground(Param.darkGreenColor);
							points++;
							break;
						}
						if (j == reponses.size()-1) { // Si on a regarde toutes les reponses (l'eleve s'est trompe)
							cList.get(i).setForeground(Color.red);
							points--;
						}

					}

				}

			}

			// Pour ne pas avoir une note negative
			if (points < 0) {
				points = 0;
			}

			String text = String.valueOf(points) + " / " + String.valueOf(reponses.size());

			JLabel lScore = new JLabel(text);
			this.add(lScore);

			Fenetre.fen.validate();


			r.setDate( new Date(System.currentTimeMillis()));

			//qcm.corriger(r);
			String message = "Validation effectu\u00E9e";
			JOptionPane.showMessageDialog(null, message, "Validation", JOptionPane.INFORMATION_MESSAGE);

		}

	}

}
